#include "stdafx.h"
//#include "mediaguids.h"
#include "fmtdefine.h"
#include <dvdmedia.h>


Fmt2SubType fmts[] = 
{
	{PIX_FMT_YUV420P ,MEDIASUBTYPE_YV12},
	
	//{PIX_FMT_YUV420P ,MEDIASUBTYPE_IYUV},
	//{PIX_FMT_YUV422P ,MEDIASUBTYPE_422P},
	//{PIX_FMT_YUV444P ,MEDIASUBTYPE_444P},
	//{PIX_FMT_YUV411P ,MEDIASUBTYPE_411P},
	//{PIX_FMT_YUV410P ,MEDIASUBTYPE_410P},
	//{PIX_FMT_YUV422  ,MEDIASUBTYPE_YUY2},
	{PIX_FMT_UYVY422 ,MEDIASUBTYPE_UYVY},
	{PIX_FMT_BGR24   ,MEDIASUBTYPE_RGB24},
//	{PIX_FMT_RGBA32  ,MEDIASUBTYPE_RGB32},
	{PIX_FMT_RGB555  ,MEDIASUBTYPE_RGB555},
	{PIX_FMT_RGB565  ,MEDIASUBTYPE_RGB565},
};



PixelFormat GetFFPixelFormat(GUID subType)
{
	for (int i = 0; i < sizeof(fmts)/sizeof(fmts[0]); i++)
	{
		Fmt2SubType ft = fmts[i];
		if(ft.subtype == subType)
			return ft.pix_fmt;
	}
	return PIX_FMT_NONE;
}

GUID GetMediaSubType(PixelFormat pixFMT)
{
	for (int i = 0; i < sizeof(fmts)/sizeof(fmts[0]); i++)
	{
		Fmt2SubType ft = fmts[i];
		if(ft.pix_fmt == pixFMT)
			return ft.subtype;
	}
	return GUID_NULL;

}

BOOL VideoAM2FM(AM_MEDIA_TYPE* pmt, PixelFormat& fmt, int &w, int &h)
{
	if(!pmt) return FALSE;
	fmt = PIX_FMT_NONE;
	w = 0;
	h = 0;	

	if(pmt->majortype != MEDIATYPE_Video) return FALSE;
	fmt = GetFFPixelFormat(pmt->subtype);	
	if(fmt == PIX_FMT_NONE) return FALSE;

	if(pmt->formattype == FORMAT_VideoInfo)
	{
		VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)pmt->pbFormat;
		w = vih->bmiHeader.biWidth;
		h = vih->bmiHeader.biHeight;		
		return TRUE;
	}
	else if( FORMAT_VideoInfo2 == pmt->formattype )
	{
		VIDEOINFOHEADER2* vih = (VIDEOINFOHEADER2*)pmt->pbFormat;
		w = vih->bmiHeader.biWidth;
		h = vih->bmiHeader.biHeight;		
		return TRUE;
	}
	return FALSE;	
}

BOOL AudioAM2FM(AM_MEDIA_TYPE* pmt, int& nchannels, int& sampleRate)
{
	nchannels = 0;
	sampleRate = 0;
	if(!pmt)return FALSE;
	if(MEDIATYPE_Audio != pmt->majortype || MEDIASUBTYPE_PCM != pmt->subtype) return FALSE;
	if(pmt->formattype == FORMAT_WaveFormatEx){
		WAVEFORMATEX* wfex = (WAVEFORMATEX*)pmt->pbFormat;
		nchannels = wfex->nChannels;
		sampleRate = wfex->nSamplesPerSec;		
		return TRUE;
	}
	return FALSE;
}